var app = getApp()
Page({
    data: {
        id:'',           // 消息id

        message_id: '',  // 消息详情id
        message_type: '', // 消息类型

        data: '',       // 消息详情
        type: '',       // 同意||拒绝
        content:'',     // 拒绝理由
    },
    // 同意或拒绝
    isdeny: function(type) {
        var self = this;
        var url = '/mytask/isdeny';                     // 帮卖确认同意或者拒绝\
        var content = this.data.content;                // 拒绝理由 
        var id = this.data.data.id;          // 消息id
        var message_type = this.data.message_type;      // 消息类型

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + url ,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: id,
                        type: type,
                        mes_type: message_type,
                        content: content
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showLoading({title: '操作成功'})
                            setTimeout(function(){
                                wx:wx.hideLoading();
                                wx.navigateBack({delta : 1})
                            })
                        }else{
                            wx.showToast({
                                image: "../../img/warning.png",
                                title: res.data.msg,
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({ 
            message_id : options.id || '',
            message_type : options.type || ''
        })

        switch (parseInt(options.type)){
            case 3:     // 帮卖提醒
                wx.setNavigationBarTitle({ title: '帮卖提醒' })
                break;
            case 4:     // 帮买提醒
                wx.setNavigationBarTitle({ title: '帮买提醒' })
                break;
            case 5:     // 帮卖邀请
                wx.setNavigationBarTitle({ title: '帮卖邀请' })
                break;
            case 6:     // 帮买邀请
                wx.setNavigationBarTitle({ title: '帮买邀请' })
                break;
            case 7:     // 委托通知 
                wx.setNavigationBarTitle({ title: '委托通知' })
                break;
        }

        this.dialog_consent = this.selectComponent("#dialog-consent");                  // 未授权手机号提示
        this.dialog_reject = this.selectComponent("#dialog-reject");                  // 未授权手机号提示
        this.getMessageDetail();
    },
    // 获取消息详情
    getMessageDetail(){
        var self = this;
        var url = '/mytask/helpconfirmdetails';
        var message_id = this.data.message_id;
        var message_type = this.data.message_type;

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid : res.data.openid,
                        token : res.data.token,
                        type : message_type,
                        id : message_id
                    },
                    success: function (res) {
                        if (res.data.code == 1) {
                            self.setData({
                                data: res.data.data
                            })
                            if (message_type == 3 || message_type == 5) {
                                self.setData({
                                    link: `../trading-entrust-sell-goods/trading-entrust-sell-goods?id=${res.data.data.id}&type=look`
                                })
                            }
                            if (message_type == 4 || message_type == 6) {
                                self.setData({
                                    link: `../trading-entrust-buy-goods/trading-entrust-buy-goods?id=${res.data.data.id}&type=look`
                                })
                            }
                        }else{
                            wx.showToast({
                                image:'../../img/warning.png',
                                title: '请求失败',
                            })
                        }
                    }
                })
            }
        })
    },
    // 显示拒绝弹窗
    __showConsentDialog() {
        this.dialog_consent.showDialog();
    },
    // 同意 - 确认按钮
    __consentEvent() {
        this.isdeny(1)
        this.dialog_consent.hideDialog();
    },
    // 同意 - 取消按钮
    __consentCancelEvent() {
        this.dialog_consent.hideDialog();
    },

    // 显示拒绝弹窗
    __showRejectDialog(){
        this.dialog_reject.showDialog();
    },
    // 拒绝理由 - 文本输入
    __textareaInput(e){
        var value = e.detail.value;
        this.setData({
            content : value
        })
    },
    // 拒绝 - 确认按钮
    __rejectEvent(){
        var content = this.data.content;
        if (!content){
            wx.showToast({
                image:"../../img/warning.png",
                title: '请输入拒绝理由',
            })
            return false;
        }
        this.isdeny(2)
        this.dialog_reject.hideDialog();
    },
    // 拒绝 - 取消按钮
    __rejectCancelEvent(){
        this.dialog_reject.hideDialog();
    },
})